package com.minshenglife.zanzan.service.power.impl;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.power.DownLoadExcelDao;
import com.minshenglife.zanzan.entity.Dept;
import com.minshenglife.zanzan.entity.power.WorkConDown;
import com.minshenglife.zanzan.service.power.DownLoadExcelService;

@Component
@Transactional
public class DownLoadExcelServiceImpl implements DownLoadExcelService {
	Logger log = LoggerFactory.getLogger(DownLoadExcelServiceImpl.class);

	@Resource
	DownLoadExcelDao downLoadExcelDao;

	@Override
	public List<List<WorkConDown>> getWorkCon(Date firstDate, Date secondDate, String dept_name) {
		// 时间
		List<String> list = this.getDete(firstDate, secondDate);
		// 日志集合
		List<List<WorkConDown>> listWordCon = new ArrayList<List<WorkConDown>>();
		// 封装好的日志
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			//System.out.println(list.get(i) + "查找数据");
			map.put("day", list.get(i));
			map.put("dept_name", dept_name);
			List<WorkConDown> li = downLoadExcelDao.getUserWork(map);
			listWordCon.add(li);
		}
		return listWordCon;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void getWorkExcel(Map<String, Object> map, List<List<WorkConDown>> results, OutputStream out) {
		// 创建一个webbook
		HSSFWorkbook wb = new HSSFWorkbook();
		// 在webbook中添加一个sheet
		HSSFSheet sheet = wb.createSheet("微家工作日志填写统计表");
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		CellRangeAddress cra = new CellRangeAddress(0, 0, 0, results.size()+2);
		//在sheet里增加合并单元格  
		sheet.addMergedRegion(cra);
		HSSFRow row2 = sheet.createRow((short) 0);
		HSSFCell cell1 = row2.createCell(0);
		cell1.setCellValue(map.get("dept_name") + "微家工作日志填写统计表");
		cell1.setCellStyle(style);
		HSSFFont font = wb.createFont();
		font.setFontName("宋体");
		font.setFontHeightInPoints((short) 14);// 设置字体大小
		// 在sheet中添加表头第0行
		HSSFRow row = sheet.createRow((int) 1);
		// 创建单元格，并设置值表头 设置表头居中
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("序号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("工号");
		cell.setCellStyle(style);
		
		cell = row.createCell(2);
		cell.setCellValue("机构");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("部门");
		cell.setCellStyle(style);
		
		cell = row.createCell(4);
		cell.setCellValue("姓名");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		Date firstDate = (Date) map.get("firstDate");
		Date secondDate = (Date) map.get("secondDate");
		List<String> listDay = this.getDete(firstDate, secondDate);
		for (int i = 0; i < results.size(); i++) {
			cell.setCellValue(listDay.get(i));
			cell.setCellStyle(style);
			cell = row.createCell(6 + i);
		}
		// 写入实体数据
		for (int i = 0; i < results.get(0).size(); i++) {
			row = sheet.createRow((int) i + 1 + 1);
			row.createCell(0).setCellValue(i + 1 + "");
			for (int j = 0; j < results.size(); j++) {
				if (j == 0) {
					row.createCell(j + 1).setCellValue(results.get(j).get(i).getEmpId());
					String deptName = results.get(j).get(i).getDept_name();
					if(deptName.indexOf("|") != -1){
						deptName = deptName.substring(0,deptName.indexOf("|"));
					}
					row.createCell(j + 2).setCellValue(deptName);
					row.createCell(j + 3).setCellValue(results.get(j).get(i).getAbbr_name());
					
					row.createCell(j + 4).setCellValue(results.get(j).get(i).getEmp_name());
					row.createCell(j + 5).setCellValue(results.get(j).get(i).getTzwork() == null ? "否" : "是");
				} else {
					row.createCell(j + 5).setCellValue(results.get(j).get(i).getTzwork() == null ? "否" : "是");
				}
			}
		}
		// 将文件存到指定位置
		try {
			wb.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public String getDept_name(Map<String, Object> map) {
		List<Dept> list = downLoadExcelDao.getDept(map);
		//System.out.println(list.size());
		StringBuffer sb = new StringBuffer("{\"msg\":[{\"state\":\"200\"}],\"data\":[");
		if (null != list) {
			for (int i = 0; i < list.size(); i++) {
				if (i != 0) {
					sb.append(",");
				}
				sb.append("{\"dept_id\":\"" + list.get(i).getDept_id() + "\",");
				sb.append("\"abbr_name\":\"" + list.get(i).getAbbr_name()+ "\",");
				sb.append("\"parent_node_id\":\"" + list.get(i).getParentNodeId()+ "\",");
				sb.append("\"dept_name\":\"" + list.get(i).getDept_name() + "\"}");
			}
		}
		sb.append("]}");
		// System.out.println(sb.toString());
		return sb.toString();
	}
	
	@Override
	public List<Dept> getDeptList(Map<String, Object> map) {
		return downLoadExcelDao.getDept(map);
	}

	/**
	 * 获取时间差的 每天的时间
	 * 
	 * @param firstDate
	 * @param secondDate
	 * @return
	 */
	public List<String> getDete(Date firstDate, Date secondDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(firstDate);
		int startDate = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(secondDate);
		int endDate = cal.get(Calendar.DAY_OF_MONTH);
		List<String> list = new ArrayList<String>();
		for (int i = startDate; i <= endDate; i++) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			cal.setTime(firstDate);
			if (i == startDate) {
				String time = formatter.format(firstDate);
				list.add(time);
				//System.out.println(time);
			} else {
				cal.add(Calendar.DATE, 1);
				firstDate = cal.getTime();
				String time = formatter.format(firstDate);
				//System.out.println(time);
				list.add(time);
			}
		}
		return list;
	}

	
}
